<?php

class VideoController extends FrontEndController
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';
	

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index', 'view'),
				'users'=>array('*'),
			)
		);
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex($category)
	{
		$content = CategoryVideos::model()->find("alias = :category", array(":category" => $category));
		if($content){
			// pagination
			$criteria=new CDbCriteria();
			$criteria->condition = 'category_id=' . $content->id;
			$pages = new EPagination(count($content->items));
			$pages->pageSize = $content->onpage!=NULL ? $content->onpage : Yii::app()->params['onpage'];
			$pages->applyLimit($criteria);
			$items = Videos::model()->findAll($criteria);
			$this->render('index',array(
				'data'=>$content,
				'pages'=>$pages,
				'items'=>$items,
			));
		} else {
			throw new CHttpException(404, 'The requested page does not exist.');
		}
	}
	public function actionView($alias)
	{
		$content = Content::model()->find("alias = :alias", array(":alias" => $alias));
		if($content){
			$this->setMeta($content, 'Content');
			$this->render('view',array(
					'data'=>$content,
			));
		} else {
			$this->actionIndex($alias);
		}
	}
	public function actionItem($category, $item)
	{
		$content = Content::model()->find("alias = :alias", array(":alias" => $item));
		if($content){
			$this->render('item',array(
				'data'=>$content,
			));
		} else {
			throw new CHttpException(404, 'The requested page does not exist.');
		}
	}

	public function actionEditMetaTags()
	{
		var_dump($_POST);
	}
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Content::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='content-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
